//
//  SecondMsgViewController.m
//  marrymarry
//
//  Created by lsj on 16/9/12.
//  Copyright © 2016年 cf. All rights reserved.
//
static NSString * messegeCellID = @"MessgeTableViewCell";
#import "SecondMsgViewController.h"
#import "FourSetViewController.h"
#import "MessgeTableViewCell.h"
#import "msgModel.h"
#import "FourSetModel.h"

@interface SecondMsgViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (strong , nonatomic) NSMutableArray * dataArry;
@property (strong , nonatomic) NSMutableArray * msgArry;
@property (strong , nonatomic) msgModel * model;
@end

@implementation SecondMsgViewController
-(NSMutableArray *)msgArry
{
    if (!_msgArry) {
        _msgArry = [NSMutableArray new];
    }
    return _msgArry;
}
-(NSMutableArray *)dataArry
{
    if (!_dataArry) {
        self.model = [msgModel new];
        _dataArry = [self.model backWithFourSetArry];
        
    }
    return _dataArry;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    self.title = @"信息";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark UITableViewDatasource,UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.msgArry.count>0) {
        return 2;
    }else
    {
        return  1;
    }
    
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.dataArry.count;
    }else
    {
        return self.msgArry.count;
    }

}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        MessgeTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:messegeCellID];
        if (!cell) {
            cell = [[MessgeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:messegeCellID];
        }
        if (self.dataArry.count>0) {
             [cell loadViewWithArry:self.dataArry andIndexPath:indexPath];
        }
       
        return cell;
    }else{
    MessgeTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:messegeCellID];
    if (!cell) {
        cell = [[MessgeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:messegeCellID];
    }
    if (self.dataArry.count>0) {
       
    }
   
    return cell;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        /** 查看礼物 **/
        if (indexPath.row == 0) {
            [self requestTheNetWith:nil andURL:WATCHGIFTS_URL andDic:@{@"userId":[Tools getValueFromNSUserDefaultsByKey:USERID]} andRequestSingStr:@"watchGiftsRequest"];
        }
        /** 查看心动 **/
        else if (indexPath.row == 1)
        {
            [self requestTheNetWith:nil andURL:WATCHWHOHEARTME_URL andDic:@{@"userId":[Tools getValueFromNSUserDefaultsByKey:USERID]} andRequestSingStr:@"watchWhoHeartMeRequest"];
        }
        /** 查看访客 **/
        else if (indexPath.row == 2)
        {
            [self requestTheNetWith:nil andURL:WATCHVISITOR_URL andDic:@{@"userId":[Tools getValueFromNSUserDefaultsByKey:USERID]} andRequestSingStr:@"watchWhoVistRequest"];
        }
        /** 查看足迹 **/
        else if (indexPath.row == 3)
        {
            [self requestTheNetWith:nil andURL:WATCHMYHEART_URL andDic:@{@"userId":[Tools getValueFromNSUserDefaultsByKey:USERID]} andRequestSingStr:@"watchMyHeartRequest"];
        }
    }
    
}

#pragma mark --------------网络请求
-(void)requestTheNetWith:(NSArray *)myRequestArry andURL:(NSString *)myurl andDic:(NSDictionary *)myDic andRequestSingStr:(NSString *)SignStr;
{
    if (myRequestArry.count>0) {
        [[LJFNetworkingManager shareManager] POST:myurl upLoadImages:myRequestArry parameters:myDic success:^(id returnData, int code, NSString *msg) {
            NSLog(@"%@",msg);
            
        } failure:^(NSError *error) {
            NSLog(@"失败");
            
        } showHUD:self.view];
        
    }else
    {
        [[LJFNetworkingManager shareManager] POST:myurl parameters:myDic success:^(id returnData, int code, NSString *msg) {
            NSLog(@"%@",msg);
            if (code == 1|| code == 0) {
                UIStoryboard * fourSB = [UIStoryboard storyboardWithName:@"FourSetViewController" bundle:nil];
                FourSetViewController * fourSetVC  = [fourSB instantiateViewControllerWithIdentifier:@"FourSetViewController"];
                FourSetModel * model = [FourSetModel new];
                /** 查看礼物 **/
                if ([SignStr isEqualToString:@"watchGiftsRequest"]) {
                    fourSetVC.isGifts= YES;
                }
                /** 查看心动 **/
                else if ([SignStr isEqualToString:@"watchWhoHeartMeRequest"])
                {
                    
                    fourSetVC.isOtherHeart =YES;
                    
                }
                /** 查看访客 **/
                else if ([SignStr isEqualToString:@"watchWhoVistRequest"])
                {
                    fourSetVC.isVister = YES;
                }
                /** 查看足迹 **/
                else if ([SignStr isEqualToString:@"watchMyHeartRequest"])
                {
                    fourSetVC.isMyheart = YES;
                    
                    
                }
                if ([SignStr isEqualToString:@"watchMyHeartRequest"]) {
                    model.isMyheart = YES;
                    if ([returnData isKindOfClass:[NSNull class]]) {
                        
                    }else{
                    fourSetVC.dataArry = [NSMutableArray arrayWithArray:[model getBackWithDataArry:returnData]];
                    }
                }else{
                    if ([returnData isKindOfClass:[NSNull class]]) {
                        
                    }else{
                fourSetVC.dataArry = [NSMutableArray arrayWithArray:[model getBackWithDataArry:returnData]];
                    }
                }
                self.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:fourSetVC animated:YES];
                self.hidesBottomBarWhenPushed = NO;
                
            }else{
                [SVProgressHUD showInfoWithStatus:msg];
            }
           
        } failure:^(NSError *error) {
            NSLog(@"%@",error);
        } showHUD:self.view];
        
    }
    
    
}






/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
